package ru.spbu.mm.diploma.client.controller;

import org.richfaces.component.html.HtmlTree;
import org.richfaces.event.NodeSelectedEvent;
import org.richfaces.model.TreeNode;
import org.richfaces.model.TreeNodeImpl;
import ru.spbu.mm.diploma.common.entity.RiskGroup;
import ru.spbu.mm.diploma.common.facade.RiskFacadeImpl;
import ru.spbu.mm.diploma.common.facade.RiskGroupFacadeImpl;
import ru.spbu.mm.diploma.common.resources.RiskOverview;

import javax.faces.context.FacesContext;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: Ksushenka
 * Date: 29.10.11
 * Time: 17:59
 * To change this template use File | Settings | File Templates.
 */
public class RiskController {
    private List<RiskOverview> risklist;

    private TreeNode rootNode = null;
    private List<String> selectedNodeChildren = new ArrayList<String>();
    private String nodeTitle;


    public TreeNode getTreeNode() {
        if (rootNode == null) {
            loadTree();
        }
        return rootNode;
    }

    public TreeNode getRootNode() {
        return rootNode;
    }

    public void setRootNode(TreeNode rootNode) {
        this.rootNode = rootNode;
    }

    public String getNodeTitle() {
        return nodeTitle;
    }

    public void setNodeTitle(String nodeTitle) {
        this.nodeTitle = nodeTitle;
    }

    public List<RiskOverview> getRisklist() {
        return risklist;
    }

    public void setRisklist(List<RiskOverview> risklist) {
        this.risklist = risklist;
    }

    private void loadTree() {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        rootNode = new TreeNodeImpl();
        risklist= RiskFacadeImpl.getInstance().getAllRiskOverview();
        addNodes();
    }

    private void addNodes() {
        List<RiskGroup> groups = RiskGroupFacadeImpl.getInstance().getAllRiskGroup();
        rootNode.setData(groups.get(0).getName());
        for (RiskGroup riskGroup : groups) {
            TreeNodeImpl<String> nodeImpl = new TreeNodeImpl<String>();
            nodeImpl.setData(riskGroup.getName());
            rootNode.addChild(riskGroup.getId(), nodeImpl);
            for (RiskOverview riskOverview : risklist) {
                if (riskOverview.getRiskGroup().getId().compareTo(riskGroup.getId())==0) {
                    TreeNodeImpl<String> child = new TreeNodeImpl<String>();
                    child.setData(riskOverview.getRisk().getName());
                    child.setParent(nodeImpl);
                    nodeImpl.addChild(riskOverview.getRisk().getId(), child);
                }
            }
        }
    }

    public void processSelection(NodeSelectedEvent event) {
        HtmlTree tree = (HtmlTree) event.getComponent();
        selectedNodeChildren.clear();
        TreeNode currentNode = tree.getModelTreeNode(tree.getRowKey());
        if (currentNode.isLeaf()) {
            selectedNodeChildren.add((String) currentNode.getData());
            nodeTitle=RiskFacadeImpl.getInstance().getRiskByName((String) tree.getRowData()).getDescription();

        } else {
            nodeTitle = RiskGroupFacadeImpl.getInstance().getGroupByName((String) tree.getRowData()).getDescription();
            Iterator<Map.Entry<Object, TreeNode>> it = currentNode.getChildren();
            while (it != null && it.hasNext()) {
                Map.Entry<Object, TreeNode> entry = it.next();
                selectedNodeChildren.add(entry.getValue().getData().toString());
            }
        }
    }


}
